﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.ICM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Mvc;
using System.Data.Entity;
using System.Linq.Dynamic;
using KPIS.GERP.Models.SYS;
using System.Data;

namespace KPIS.GERP.WCF.GIMS
{

    [ServiceContract]
    public interface IPurchaseOrderItemService
    {
        [OperationContract]
        IEnumerable<PurchaseOrderItem> Select(PurchaseOrderItem filter = null, List<string> includeSubObjectString = null);

        [OperationContract]
        List<SelectListItem> SelectList(PurchaseOrderItem criteriaModel = null);

        [OperationContract]
        int Insert(PurchaseOrderItem model, int createByUserId);

        [OperationContract]
        void Update(PurchaseOrderItem model, int updateByUserId);

        [OperationContract]
        void Delete(int Id, int deleteByUserId);
    }
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "PurchaseOrderItemItemService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select PurchaseOrderItemItemService.svc or PurchaseOrderItemItemService.svc.cs at the Solution Explorer and start debugging.
    public class PurchaseOrderItemService : IPurchaseOrderItemService
    {
        public IEnumerable<PurchaseOrderItem> Select(PurchaseOrderItem criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<PurchaseOrderItem> query = context.PurchaseOrderItems;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //throw new NotImplementedException();
        }

        public List<SelectListItem> SelectList(PurchaseOrderItem criteriaModel = null)
        {
            var list = new List<SelectListItem>();

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<PurchaseOrderItem> query = context.PurchaseOrderItems;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var PurchaseOrderItem in x)
                    {
                        var item = new SelectListItem { Text = PurchaseOrderItem.PoItemId.ToString(), Value = PurchaseOrderItem.PoItemId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
            //throw new NotImplementedException();
        }

        public int Insert(PurchaseOrderItem model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    //model.RecordStatus = RecordStatus.WaitStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.PurchaseOrderItems.Add(model);
                    context.SaveChanges();

                    return model.PoId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //throw new NotImplementedException();
        }

        public void Update(PurchaseOrderItem model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //throw new NotImplementedException();
        }

        public void Delete(int Id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.PurchaseOrderItems.Find(Id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                   // modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //throw new NotImplementedException();
        }
    }
}
